
jQuery(document).ready(function() {
	
	var orgData = {};
	$.ajax({
		async : false,
		url : projectPath + "user/getCurrentUser.do",
		data : {},
		dataType : "json",
		success : function(data) {
			orgData = data;
			$('#orgSelect').val(orgData.orgname);
			$('#orgSelect').attr('data', orgData.orgcode);
		}
	});
	
	bindOrg2('orgSelect');
//	bindOrg("orgSelect1","2","0","");
	
	upload("upload","","","","");
		
	//失效实现显示
	$("#sxRadio").click(function(){		
		var a= $('#sxRadio input:radio:checked').val();
		if(a=="1"){
			$("#failDate").removeClass("hidden");
			$("#sx").removeClass("hidden");
		}else{
			$("#failDate").addClass("hidden");
			$("#sx").addClass("hidden");
		}
	});
	
	//失效时间范围
	$("#publishTime").change(function(){
		var time=new Date();
		if (new Date($(this).val()) >= time) {
			$("#asofDate").attr('min', $(this).val());
			$("#failDate").attr('min', $(this).val());
		} else {
			var year = time.getFullYear();
			var month = time.getMonth() + 1;
			var day = time.getDate();
			var dayStr = day > 9 ? day : '0' + day;
			var monthStr = month > 9 ? month : '0' + month;
			dateStr = year + "-" + monthStr + "-" + dayStr;
			$("#asofDate").attr('min', dateStr);
			$("#failDate").attr('min', dateStr);
		}				
		
	});
	
	$("#asofDate").change(function(){
		$("#failDate").attr('max', $(this).val());
	});
	
});


//初始化上传插件(上传)
function upload(id,noticeId){
	$("#"+id).fileinput({ 
		language: 'zh', //设置语言
		browseLable:"选择上传...",
		enctype: 'multipart/form-data',
		allowedFileExtensions : ['jpg', 'png','gif','txt','sql','xlsx','xls','pdf','pptx','docx','doc','jpeg'],//接收的文件后缀
        uploadUrl:basePath()+"upload.do",
        showUpload: false, //是否显示上传按钮
        showCaption: false,//是否显示标题
        browseClass: "btn btn-primary", //按钮样式          
        overwriteInitial:false,
        previewFileIcon: '<i class="fa fa-file"></i>',
        uploadAsync:false, 
        initialPreviewShowDelete:true, 
        maxFileCount: 5,  
        maxFileSize: 1024*10,      
        uploadExtraData: function(previewId, index){ //额外参数 返回json数组 
        	return{
        		'busscode':'01'
        	}
        },       
             	            		  				 	                             				 			
	}).on("filebatchuploadsuccess", function(event, data) {
		var file=data.response.datas;
		var fileId="";   	
		for (var i = 0; i < file.length; i++) {
			fileId += file[i].fileid + ",";
		}
	    fileId = fileId.substring(0, fileId.length - 1);
    	//当上传成功回调函数     											
		$.ajax({
			method : 'post',
			url : basePath() + 'notice/fileId.do',
			data:{
				fileId:fileId,
				noticeId:noticeId
			},
			dataType : "json",
			success : function(data) {				
			}
		})	 
    	parent.refreshData();		//刷新数据
	});
	
}

function save(){	
	var noticeData = $("#noticeForm").serializeObject();
	noticeData.orgCode = $("#orgSelect").attr("data");
	if (!check(noticeData))
		return true;
	$.ajax({
		method : 'post',
		url : basePath() + 'notice/save.do',
		data : JSON.stringify(noticeData),
		dataType : "json",
		contentType : "application/json",
		success : function(data) {
			var noticeId=data.noticeId;
			if (data.result == 1) {
				if($('#upload').fileinput("getFilesCount")>0){																				 
					upload("upload",noticeId);
					$('#upload').fileinput('upload');
				}else{
					parent.refreshData();		//刷新数据
				}								
				
			} else {
				window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.info);
			}
		}
	});
}	

function check(data) {
	var result = true;
	var mustStr = "title,symbol,contents,orgCode,publishTime,asofDate";
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for (var i = 0; i < mustStrs.length; i++) {
		var obj = $('[name="' + mustStrs[i] + '"]');
		var obj1 = obj.attr("placeholder");
		if (obj.val() == "") {
			strMsg += obj1.substring(0, 3)
					+ "<span style='color:red;'><strong>"
					+ obj1.substring(3, obj1.lengh) + "</strong></span>！<br />";
			result = false;
		}
	}
//	var a = $("[name='range']:checked").val();
//	if (a == null) {
//		strMsg += "请选择<span style='color:red;'><strong>发布范围</strong></span>！<br />"
//		result = false;
//	}
	if($("[name='top'][value='1']").prop("checked") && $("#failDate").val()==""){
		strMsg += "请选择<span style='color:red;'><strong>置顶失效时间</strong></span>！<br />"
		result = false;	
	}
	if (strMsg != "") {
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.error);
	}
	return result;
}

function path(){
	var curWwwPath = window.document.location.href;
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	var localhostPath = curWwwPath.substring(0, pos);
	return localhostPath+"/"
}
